//import fl.controls.TextInput;
//import fl.controls.Button;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.NetStatusEvent;
import flash.net.NetConnection;
import flash.net.Responder;

class Login extends Panel
{
	var logo:Logo;
	var label:fl.controls.Label;
	var input:fl.controls.TextInput;
	var btn:fl.controls.Button;
	var connection:NetConnection;
	var console:Console;
	var container:Sprite;
	public function new(connection, console)
	{
		super();
		this.connection = connection;
		this.console = console;
		container = createView();
		minWidth=container.width-10;
		minHeight=container.height+45;
		addChild(container);
		addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
	}
	function createView()
	{
		var container = new Sprite();
		logo = new Logo();
		container.addChild(logo);
		
		label = new fl.controls.Label();
		label.height=20;
		label.text = 'user name:';
		container.addChild(label);
		
		input = new fl.controls.TextInput();
		input.width=100;
		input.height=20;
		input.restrict="0-9a-zA-Z";
		container.addChild(input);
		
		btn = new fl.controls.Button();
		btn.height=20;
		btn.label="enter";
		btn.addEventListener(flash.events.MouseEvent.CLICK, onBtn);
		container.addChild(btn);
		
		label.x = (container.width - (label.width + 10 + input.width + 10 + btn.width))/2;
		input.x = label.x+ label.width + 10;
		btn.x = input.x+ input.width + 10;
	
		label.y = input.y = btn.y = logo.y+logo.height+50;
		
		return container;
	}
	function onAddedToStage(event:Event):Void
	{
		this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		stage.addEventListener(Event.RESIZE, onStageResize);
		setSize(stage.stageWidth, stage.stageHeight-250);
		container.x = (w - container.width+55)/2;
		container.y = (h - container.height-80)/2;
		onStageResize(null);
		//onColorChange(0, true);
	}
	function onStageResize(event:Event):Void
	{
		if(visible && stage!=null)
		{
			x = (stage.stageWidth - this.width)/2;
			y = (stage.stageHeight - this.height)/2;
		}
	}
	override function onResize(event:MouseEvent):Void
	{
		super.onResize(event);//setSize(stage.mouseX-this.x+15, stage.mouseY-this.y+15);
		container.x = (w - container.width+55)/2;
		container.y = (h - container.height-80)/2+200;
	}
	function onRemovedFromStage(event:Event):Void
	{
		this.removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		stage.removeEventListener(Event.RESIZE, onStageResize);
		addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
	}
	function onBtn(event:flash.events.MouseEvent):Void
	{
		if(connection.connected)
		{
			connection.call("getLogin", new Responder(onGetLogin, onError), input.text);
		}
		else
		{
			connection.client=this;
			connection.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
			console.log("Connecting to:"+Main.path);
			connection.connect(Main.path);
		}
	}
	function onNetStatus(event:NetStatusEvent):Void
	{
		//console.log(event);
		if(event.info.code =='NetConnection.Connect.Success')
		{
			connection.call("getLogin", new Responder(onGetLogin, onError), input.text);
		}
	}
	
	function onGetLogin(result:Bool)
	{
		if(result)
		{
			connection.removeEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
			dispatchEvent(new Event("login"));
		}
		else
			console.log("name is taken");
	}
	function onError(obj:Dynamic)
	{
		console.log(obj);
		console.log(obj.level);
		console.log(obj.code);
		console.log(obj.description);
	}
	public function getUserName():String
	{
		return input.text;
	}
	public function setUserName(v):Void
	{
		input.text=v;
	}
}
